package com.xinou.daily_report.utils;

import com.xinou.common.util.SpringContextBeanUtil;
import com.xinou.sso.dto.RoleDTO;
import com.xinou.sso.dto.UserDTO;
import com.xinou.sso.mapper.SysUserMapper;
import com.xinou.sso.util.ShiroUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.*;
import java.util.stream.Collectors;

/**
 * xx<br>
 *
 * @author lizhongyuan
 * @date 2021/6/22
 */
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DailyShiroUtil {

    private final StringRedisTemplate stringRedisTemplate;

    private static final String CONFIG_LEAD_ROLE = "config:lead-role";
    private static final String CONFIG_BIG_LEAD = "config:big-lead";
    private static final String CONFIG_GROUP_LEADER = "config:group-leader";
    private static final String CONFIG_ASSISTANT = "config:assistant";
    private static final String CONFIG_STAFF_MEMBER = "config:staff-member";

    private static Set<String> roleNames;
    private static String bigLeadName;
    private static String assistantName;
    private static String groupLeaderName;
    private static String staffMemberName;
    private static Set<String> bigLeadAssistantNames;

    @PostConstruct
    void init() {
//        roleNames = stringRedisTemplate.opsForSet().members(CONFIG_LEAD_ROLE);
//        bigLeadName = stringRedisTemplate.opsForValue().get(CONFIG_BIG_LEAD);
//        assistantName=stringRedisTemplate.opsForValue().get(CONFIG_ASSISTANT);
//        groupLeaderName=stringRedisTemplate.opsForValue().get(CONFIG_GROUP_LEADER);
//        staffMemberName=stringRedisTemplate.opsForValue().get(CONFIG_STAFF_MEMBER);
//        bigLeadAssistantNames= new HashSet<>();
//        bigLeadAssistantNames.add(bigLeadName);
//        bigLeadAssistantNames.add(assistantName);
        bigLeadName = "领导";
        assistantName = "助理";
        groupLeaderName = "组长";
        staffMemberName = "员工";
        roleNames = new HashSet<>();
        roleNames.add(bigLeadName);
        roleNames.add(assistantName);
        roleNames.add(groupLeaderName);
        bigLeadAssistantNames= new HashSet<>();
        bigLeadAssistantNames.add(bigLeadName);
        bigLeadAssistantNames.add(assistantName);
    }

    public static boolean isBigLead() {
       return isLead(bigLeadName);
    }

    public static boolean needComment(List<String> roles){
        return isLead(roles);
    }

    public static boolean isBigLead(List<String> roles){
        return roles != null && roles.contains(bigLeadName);
    }

    public static boolean isLead(List<String> roles){
        return roles != null && roles.stream().anyMatch(role -> roleNames.contains(role));
    }

    public static boolean isStaff(List<String> roles){
        return roles != null && roles.contains(staffMemberName);
    }

    public static boolean isAssistant(List<String> roles){
        return roles != null && roles.contains(assistantName);
    }

    public static boolean isAssistant(){
        UserDTO dto = ShiroUtil.getUserDto();
        if (dto.isAdmin()) {
            return true;
        }
        List<RoleDTO> roles = dto.getRoles();
        if (roles == null || roles.isEmpty()) {
            return false;
        }
        return roles.stream().anyMatch(role -> Objects.equals(assistantName, role.getRoleName()));
    }

    public static boolean isGroupLeader(List<String> roles){
        return roles != null && roles.contains(groupLeaderName);
    }

    private static boolean isLead(String roleName) {
        UserDTO dto = ShiroUtil.getUserDto();
        if (dto.isAdmin()) {
            return true;
        }
        List<RoleDTO> roles = dto.getRoles();
        if (roles == null || roles.isEmpty()) {
            return false;
        }
        return roles.stream().anyMatch(role -> Objects.equals(roleName, role.getRoleName()));
    }

    public static boolean isBigLeadAssistant() {
        return isLead(bigLeadAssistantNames);
    }

    public static boolean isLead() {
        return isLead(roleNames);
    }

    private static boolean isLead(Set<String> roleNames) {
        UserDTO dto = ShiroUtil.getUserDto();
        if (dto.isAdmin()) {
            return true;
        }
        List<RoleDTO> roles = dto.getRoles();
        if (roles == null || roles.isEmpty()) {
            return false;
        }
        return roles.stream().anyMatch(role -> roleNames.contains(role.getRoleName()));
    }

    public static Set<Integer> roles() {
        UserDTO dto = ShiroUtil.getUserDto();
        Set<Integer> role = new HashSet<>();
        if (dto.isAdmin()) {
            role.add(0);
            role.add(1);
            role.add(2);
            role.add(3);
            return role;
        }
        List<RoleDTO> roles = dto.getRoles();
        if (roles == null || roles.isEmpty()) {
            return new HashSet<>();
        }
        Set<String> roleNames = roles.stream().map(RoleDTO::getRoleName).collect(Collectors.toSet());
        if (roleNames.contains(staffMemberName)) {
            role.add(0);
        }
        if (roleNames.contains(groupLeaderName)) {
            role.add(1);
        }
        if (roleNames.contains(assistantName)) {
            role.add(2);
        }
        if (roleNames.contains(bigLeadName)) {
            role.add(3);
        }
        return role;
    }

//    public static void restLeadRole() {
//        roleNames.clear();
//        StringRedisTemplate redisTemplate = SpringContextBeanUtil.getBean(StringRedisTemplate.class);
//        roleNames = redisTemplate.opsForSet().members(CONFIG_LEAD_ROLE);
//    }

}
